define([
	"UtilDir/util",
	"UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
	"Date","DateCN", "css!DateCss",
	"EasyUI","EasyUI-lang"
    ],function(Util, Tool, SelectOrg, SearchBlock){

//  var sysPath =  getServer() + "/static/app/cdqh/qhstandarddataoutput";
//
//  var qhDrillOverviewFormHtml = sysPath + "/views/qhDrillOverviewForm.html";
//  var qhDrillHistogramFormHtml = sysPath + "/views/qhDrillHistogramForm.html";
//  var qhStandardPenetrationFormHtml = sysPath + "/views/qhStandardPenetrationForm.html";
//  var qhSituShearWaveFormHtml = sysPath + "/views/qhSituShearWaveForm.html";
//  var qhSoilDynamicFormHtml = sysPath + "/views/qhSoilDynamicForm.html";
//  var qhSeismicFortificationFormHtml = sysPath + "/views/qhSeismicFortificationForm.html";

	var getQhDrillOverviewController = function(){
		return getServer() + "/qh/qhDrillOverviews";
	};

	var getQhCriteriaQueriesController = function () {
        return getServer()  + "/qh/zfzxCriteriaQueries";
    };

	/**
	 * 页面初始化
	 */
	var init = function(){
        initSearchBlock();
        queryBtnBind();
        initcreateProvince();
		initReviewStatusSelect();		
	};
    var initcreateProvince = function () {
        var html = "";
        $("#citySelect").append(html);
        $("#areaSelect").append(html);
        $.ajax({
            url: "/divisions/root/subdivisions",
            contentType: "application/json",
            type: "get",
            success: function (data) {
                $.each(data, function (idx, item) {
                    html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                });
                $("#provinceSelect").append(html);
            }
        });
        $("#provinceSelect").change(function () {
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#citySelect option").remove();
            $("#citySelect").append(html);
            $("#areaSelect option").remove();
            $("#areaSelect").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            if ($(this).val() == "北京市" || $(this).val() == "天津市" || $(this).val() == "上海市" || $(this).val() == "重庆市") {
                $("#citySelect").append("<option value='" + $(this).val() + "' exid='" + divisionId + "'>" + $(this).val() + "</option>");
                return;
            }
            $.ajax({
                url: "/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#citySelect").append(html);
                }
            });
        });
        $("#citySelect").change(function () {
            if ($(this).val() == "") return;
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#areaSelect option").remove();
            $("#areaSelect").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            $.ajax({
                url: "/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#areaSelect").append(html);
                }
            });
        });
    }
    var initcreateProvinceForm = function () {
        var html = "";
        $("#city").append(html);
        $("#area").append(html);
        $.ajax({
            url: "/divisions/root/subdivisions",
            contentType: "application/json",
            type: "get",
            success: function (data) {
                $.each(data, function (idx, item) {
                    html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                });
                $("#province").append(html);
            }
        });
        $("#province").change(function () {
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#city option").remove();
            $("#city").append(html);
            $("#area option").remove();
            $("#area").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            if ($(this).val() == "北京市" || $(this).val() == "天津市" || $(this).val() == "上海市" || $(this).val() == "重庆市") {
                $("#city").append("<option value='" + $(this).val() + "' exid='" + divisionId + "'>" + $(this).val() + "</option>");
                return;
            }
            $.ajax({
                url: "/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#city").append(html);
                }
            });
        });
        $("#city").change(function () {
            if ($(this).val() == "") return;
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#area option").remove();
            $("#area").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            $.ajax({
                url: "/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#area").append(html);
                }
            });
        });
    }

	var initReviewStatusSelect = function () {
		$.ajax({
			url: getQhDrillOverviewController() + "/getValidDictItemsByDictCode/" + "reviewStatusSelect",
			type: "get",
			success: function (data) {
				var mySelect = document.getElementById("reviewStatusSelect");
				mySelect.add(new Option("请选择", ''), 0);
				for (var i = 0; i < data.length; i++) {
					var name = data[i].dictItemName;
					var code = data[i].dictItemCode;
					mySelect.add(new Option(name, code));
				}
			}
		});
	};

	var initSearchBlock = function(){
        SearchBlock.init("searchBlock");
        require(["jquery", "Date", "DateCN", "css!DateCss"], function ($) {
            $("#complex1").datetimepicker({
                //设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
                language: "cn",
                //输出格式化
                format: 'yyyy',
                //直接选择‘今天’
                todayBtn: true,
                weekStart: 1,
                // 设置最精确的时间选择视图
                startView: 4,
                //设置最精确的时间选择视图
                minView: 4,
//              minView: 'month',
                //高亮当天日期
                todayHighlight: true,
                //选择完毕后自动关闭
                autoclose: true
            });
            /* 删除按钮 */
            $("#complex_show1").on("click", function () {
                $('#complex1').datetimepicker('show');
            });
            /* 显示按钮 */
            $("#complex_del1").on("click", function () {
                $("#complex1").val("");
            });

            $("#complex2").datetimepicker({
                //设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
                language: "cn",
                //输出格式化
                format: 'yyyy',
                weekStart: 1,
                // 设置最精确的时间选择视图
                startView: 4,
                //直接选择‘今天’
                todayBtn: true,
                //设置最精确的时间选择视图
                minView: 4,
                //minView: 'month',
                //高亮当天日期
                todayHighlight: true,
                //选择完毕后自动关闭
                autoclose: true
            });
            /* 删除按钮 */
            $("#complex_show2").on("click", function () {
                $('#complex2').datetimepicker('show');
            });
            /* 显示按钮 */
            $("#complex_del2").on("click", function () {
                $("#complex2").val("");
            });
        })
	};
	var queryBtnBind = function(){
		$("#complex1").change(function () {
            if ($("#complex1").val() != "" && $("#complex2").val() != "") {
                if ($("#complex1").val() > $("#complex2").val()) {
                    Util.alert("开始日期必须小于等于结束日期", function () {
                        $("#complex1").val("");
                        $("#complex2").val("");
                    });
                    return;
                }
            }
        });
        $("#complex2").change(function () {
            if ($("#complex1").val() != "" && $("#complex2").val() != "") {
                if ($("#complex1").val() > $("#complex2").val()) {
                    Util.alert("开始日期必须小于等于结束日期", function () {
                        $("#complex1").val("");
                        $("#complex2").val("");
                    });
                    return;
                }
            }
        });
        $("#drillDepthStart").change(function () {
            if ($("#drillDepthStart").val() != "" && $("#drillDepthEnd").val() != "") {
                if ($("#drillDepthStart").val() > $("#drillDepthEnd").val()) {
                    Util.alert("开始终孔深度必须小于等于结束终孔深度", function () {
                        $("#drillDepthStart").val("");
                        $("#drillDepthEnd").val("");
                    });
                    return;
                }
            }
        });
        $("#drillDepthEnd").change(function () {
            if ($("#drillDepthStart").val() != "" && $("#drillDepthEnd").val() != "") {
                if ($("#drillDepthStart").val() > $("#drillDepthEnd").val()) {
                    Util.alert("开始终孔深度必须小于等于结束终孔深度", function () {
                        $("#drillDepthStart").val("");
                        $("#drillDepthEnd").val("");
                    });
                    return;
                }
            }
        });
        $("#batchExport").click(function () {
            batchExportForm();
        });
        $("#btnReset").click(function () {
			$("#reportNameCondition").val("");
			$("#projectNameCondition").val("");
            $("#provinceSelect").val("");
            $("#citySelect").val("");
            $("#areaSelect").val("");
            $("#town").val("");
			$("#drillCodeCondition").val("");
            $("#reviewStatusSelect").val("");
            $("#siteClass").val("");
            $("#complex1").val("");
        	$("#complex2").val("");
        	$("#drillDepthStart").val("");
        	$("#drillDepthEnd").val("");
		});
	};

	//批量导出
    var batchExportForm = function () {
        $.ajax({
            url:getQhDrillOverviewController() + "/queryQhDrillOverviewsNoPage",
            contentType:"application/json",
            type: "post",
            data:JSON.stringify({
                reportName: $("#reportNameCondition").val(),
                projectName: $("#projectNameCondition").val(),
                province: $("#provinceSelect").val(),
                city: $("#citySelect").val(),
                area: $("#areaSelect").val(),
                town: $("#town").val(),
                drillCode: $("#drillCodeCondition").val(),
                reviewStatus: $("#reviewStatusSelect").val(),
                siteClass:$("#siteClass").val(),
                poreFormingBegin: $("#complex1").val(),
                poreFormingEnd: $("#complex2").val(),
                drillDepthBegin: $("#drillDepthStart").val(),
                drillDepthEnd: $("#drillDepthEnd").val()

            }),
            success: function(res) {
                if(res.data.length < 1){
                    Util.alert("没有数据需要导出");
                    return;
                }
                var ids = "";
                for(var i = 0; i < res.data.length; ++i) {
                    ids += res.data[i].id + ",";
                }

                ids = ids.substr(0, ids.length - 1);
                window.location.href = getQhCriteriaQueriesController() + "/exportFile/" + ids;
            }
        })
    };
	return {
		init:init
	};
});
